#encoding=utf8
'''
Created on Nov 7, 2014

@author: jingyang <jingyang@smalltreemedia.com>
'''
from django import forms

from core.widgets import CustomTextarea
from iap_info.models import IapInfo, PublicKey


class IapInfoForm(forms.ModelForm):

    error_messages = {'required': 'This field is required.'}

    local_doc = forms.CharField(required=False, max_length=1000,
        label=u'IAP SS Path (Local)', widget=CustomTextarea)
    remote_doc = forms.CharField(required=False, max_length=1000,
        label=u'IAP SS Path (Dropbox)', widget=CustomTextarea)

    class Meta:
        model = IapInfo

    def clean_local_doc(self):
        app = self.cleaned_data.get('app')
        local_doc = self.cleaned_data.get('local_doc')
        if app:
            if app.app_store == 'iTunes App Store' or app.app_store == 'Amazon App Store':
                if not local_doc:
                    raise forms.ValidationError(self.error_messages['required'])
        return local_doc

    def clean_remote_doc(self):
        app = self.cleaned_data.get('app')
        remote_doc = self.cleaned_data.get('remote_doc')
        if app:
            if app.app_store == ('iTunes App Store' or 'Amazon App Store'):
                if not remote_doc:
                    raise forms.ValidationError(self.error_messages['required'])
        return remote_doc
